Completed
Push — master ( 3864a1...e839e7 )
by
unknown
03:14
created

duplicate.js ➔ duplicate   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 1
c 3
b 2
f 0
nc 1
dl 0
loc 44
rs 8.8571
nop 6

1 Function

Rating   Name   Duplication   Size   Complexity  
C duplicate.js ➔ ... ➔ ??? 0 40 7
1
import path from 'path'
2
import extend from 'extend'
3
4
import {
5
  abeExtend,
6
  Manager,
7
  config,
8
  coreUtils,
9
  cmsData,
10
  cmsOperations
11
} from '../../'
12
13
const duplicate = function(oldPostUrl, template, newPath, name, req, isUpdate = false) {
14
  const p = new Promise((resolve, reject) => {
15
    abeExtend.hooks.instance.trigger('beforeDuplicate', oldPostUrl, template, newPath, name, req, isUpdate)
16
17
    let json = {}
18
    let revisions = []
19
    const newPostUrl = path.join(newPath, coreUtils.slug.clean(name))
20
    if(oldPostUrl != null) {
21
      const files = Manager.instance.getList()
22
      const oldPostDataPath = path.join(config.root, config.data.url, oldPostUrl.replace('.' + config.files.templates.extension, '.json'))
23
      let posts = []
0 ignored issues
show
Unused Code introduced by
The assignment to variable posts seems to be never used. Consider removing it.
Loading history...
24
      posts = coreUtils.array.filter(files, 'path', oldPostDataPath)
25
26
      if(posts.length > 0 && posts[0].revisions != null) {
27
        revisions = posts[0].revisions
28
        if(revisions != null && revisions[0] != null) {
29
          json = cmsData.file.get(revisions[0].path)
30
          json = extend(true, json, req.body)
31
32
          delete json.abe_meta
33
        }
34
      }
35
    }
36
37
    abeExtend.hooks.instance.trigger('afterDuplicate', json, oldPostUrl, template, newPath, name, req, isUpdate)
38
39
    var pCreate = cmsOperations.create(template, newPath, name, req, json, (isUpdate) ? false : true)
40
    pCreate.then((resSave) => {
41
      if (isUpdate && oldPostUrl !== newPostUrl) {
42
        abeExtend.hooks.instance.trigger('beforeUpdate', json, oldPostUrl, template, newPath, name, req, isUpdate)
43
        cmsOperations.remove.remove(oldPostUrl)
44
      }
45
      resolve(resSave)
46
    },
47
    () => {
48
      reject()
49
    }).catch(function(e) {
50
      console.error('[ERROR] abe-duplicate.js', e)
51
      reject()
52
    })
53
  })
54
55
  return p
56
}
57
58
export default duplicate